package yxy.game.pm2.module.kow;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.kow.KowHandle.KowRankLevel;

import java.util.ArrayList;

public class KowPlayerModule extends PlayerModule {
	// bean===========================================================================
	private String code;// 赛季code
	private DateTime updateTime;
	private int group;// 玩家分组
	private int times;// 挑战次数
	private int refreshtimes;// 刷新次数
	private ArrayList<String> oppo = new ArrayList<>();// 对手
	private KowRankLevel nowLevel = KowRankLevel.None;
	private KowRankLevel bestLevel = KowRankLevel.None;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public DateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	public int getGroup() {
		return group;
	}

	public void setGroup(int group) {
		this.group = group;
	}

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public int getRefreshtimes() {
		return refreshtimes;
	}

	public void setRefreshtimes(int refreshtimes) {
		this.refreshtimes = refreshtimes;
	}

	public ArrayList<String> getOppo() {
		return oppo;
	}

	public void setOppo(ArrayList<String> oppo) {
		this.oppo = oppo;
	}

	public KowRankLevel getNowLevel() {
		return nowLevel;
	}

	public void setNowLevel(KowRankLevel nowLevel) {
		this.nowLevel = nowLevel;
	}

	public KowRankLevel getBestLevel() {
		return bestLevel;
	}

	public void setBestLevel(KowRankLevel bestLevel) {
		this.bestLevel = bestLevel;
	}

	// func===========================================================================

	private void update() {
		DateTime now = DateTime.now();
		if (getUpdateTime() == null || !new LocalDate(now).equals(new LocalDate(getUpdateTime()))) {
			try {
				if (tryLock()) {
					setUpdateTime(now);
					setTimes(0);
					setRefreshtimes(0);
					set();
				}
			} catch (Exception e) {
			} finally {
				unlock();
			}
		}
	}

	public void rankLevelUp(KowRankLevel level) {
		setNowLevel(level);
		if (level.getNumber() > 0 && level.getNumber() < getBestLevel().getNumber()) {
			setBestLevel(level);
		}
	}
	// redis==========================================================================

	static public String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:KOW", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public KowPlayerModule get(String playerCode) {
		try {
			KowPlayerModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), KowPlayerModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			// TODO 创建模块需要移植等级模块开启处理里
			if (module == null) {
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private KowPlayerModule create(String playerCode) {
		KowPlayerModule module = new KowPlayerModule();
		module.setPlayerCode(playerCode);
		module.setCode("xx");
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				KowPlayerModule kowPlayerModule = getv(key(playerCode));
				if(kowPlayerModule != null)MySQLCacheDAO.setV(key, kowPlayerModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

	public int remainTimes() {
		int max = fomula.天下第一积分赛每日可挑战次数.intValue();
		int times = max - getTimes();
		return times < 0 ? 0 : times;
	}
}